<?php
session_start();
require('../conn.php');
require('../functions.php');
require('admin.php');

//分页查询操作
$page = $_GET['page'];
if (is_numeric($page) == false) {
    $page = 1;
    $page_1 = 1;
    $min = 0;
    $page_2 = $page + 1;
} else {
    if ($page > 1) {
        $page_2 = $page + 1;
        $min = ($page - 1) * 20;
        $page_1 = $page - 1;
    } else {
        $page_1 = 1;
        $min = 0;
        $page_2 = 2;
    }
}
$max = $page * 20;
$sql = "select * from `news` WHERE `top` = 1 order by `day` desc,`time` desc limit {$min},{$max}";
//删除文章
if($_GET['action'] == 'del'){
    $id = $_GET['aid'];
    $uid = $_GET['uid'];
    //删除文章扣分
//    $score = 30;
//    $res = $mysql->execute("update `user` set `total` = `total` - {$score} WHERE `id` ={$uid}");
    //记入send表
//    $send = [
//        'nid' => $id,
//        'uid' => $uid,
//        'content' => '文章违规被删除',
//        'day' => date('Y-m-d H:i:s',time()),
//        'score' => $score
//    ];
//    $sendSql = "insert into `send`".arr2s($send);
//    $res = $mysql->execute($sendSql);
    $res = $mysql->execute("delete from `news` WHERE `id` = {$id}");
    if($res){
        echo "<script>alert('删除成功')</script>";
        echo "<script>location.href='./publish_list.php'</script>";
    }
}
//禁言用户
if($_GET['action'] == 'ban'){
    $uid = $_GET['uid'];
    $day = date("Y-m-d",time());
    $res = $mysql->execute("update `user` set `ban` = 1,`ban_day` = '{$day}' WHERE `id` = {$uid}");
    if($res){
        echo "<script>alert('禁言成功')</script>";
        echo "<script>location.href='./publish_list.php'</script>";
    }
}
if ($_GET['do'] == 'search') {
    $search_title = guolv($_GET['search_title']);
    $sql = "select * from `news` where  `content` LIKE  '%{$search_title}%' AND `top` = 1  order by `day` desc,`time` desc limit 35";
}else if($_GET['order'] == 'type') {
    $type = guolv($_GET['typeid']);
    $sql = "select * from `news` where  `type` = {$type} AND `top` = 1  order by `day` desc,`time` desc limit {$min},{$max}";
}else if($_GET['order'] == 'code'){
	$code = guolv($_GET['typeid']);
    $sql = "select * from `news` where  `code` = {$code} AND `top` = 1  order by `day` desc,`time` desc limit {$min},{$max}";
}
$rows = $mysql->query($sql);
?>

<?php include('head.php') ?>
<div class="container-fluid">
    <div class="row-fluid">
        <?php
        include('left.php'); ?>
        <div class="span9">
            <h1 class="page-title" style="color:green">
                已发布列表
            </h1>
            <div class="well">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <div class="well">
                            <form class="" action="" method="get">
                                <input type="hidden" name="do" value="search">
                                <input type="text" class="search-query" placeholder="搜索文章标题+回车" name="search_title">
                            </form>
                            <div>
								    	<a href="?">全部</a> |
								    	 <a href="?order=type&typeid=1">视频</a> | 
								      
								        <a href="?order=type&typeid=2">美图</a> | 
								      
								        <a href="?order=type&typeid=3">纯文</a> || 
								      
								        <a href="?order=code&typeid=1">搞笑图片</a> | 
								      
								        <a href="?order=code&typeid=2">搞笑视频 </a> | 
								      
								        <a href="?order=code&typeid=3">搞笑段子</a> | 
								      
								        <a href="?order=code&typeid=4">女神来了</a> | 
								      
								        <a href="?order=code&typeid=5">有胸器</a> | 
								      
								        <a href="?order=code&typeid=6">街头采访</a> | 
								      
								        <a href="?order=code&typeid=7">综艺娱乐</a> | 
								      
								        <a href="?order=code&typeid=8">心灵美文</a> | 
								      
								        <a href="?order=code&typeid=9">奇闻异事</a> | 
								      
								        <a href="?order=code&typeid=10">爆笑GIF</a> | 
								      
								        <a href="?order=code&typeid=11">内涵囧图</a> | 
								      
								        <a href="?order=code&typeid=12">体育赛事</a> 
							</div>
                            <table class="table">
                                <form class="navbar-search pull-left" action="article.php" method="post">
                                    <thead>
                                    <tr>
                                        <th>用户id</th>
                                        <th>用户名</th>
                                        <th>发布日期</th>
                                        <th style="width: 50%;">发表内容</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php
                                    foreach ($rows as $row) {
										 if ($row['img']) {
                                            $content = "<span style='color: red'>图</span>" . $row['content'];
                                        }else {
                                             $content = $row['content'];
                                         }
                                        if($row['flag'] == 2){
                                        	$content = "<span style='color: red'>长图</span>" . $row['content'];
                                        }else if($row['flag'] == 3){
                                        	$content = "<span style='color: red'>动态图</span>" . $row['content'];
                                        }else if($row['flag'] == 4){
                                        	$content = "<span style='color: red'>视频</span>" . $row['content'];
                                        }
                                        $btn = "
		  <a class=\"btn\" href=\"dynamic_detail.php?aid={$row['id']}\" target=\"_blank\">查看</a>
			<a class=\"btn\" href=\"?aid={$row['id']}&uid={$row['uid']}&action=del\" style=\"color:green\">删除</a>
			<a class=\"btn\" href=\"?uid={$row['uid']}&action=ban\" style=\"\">禁言</a>
			";
                                        $user = $mysql->query("select * from `user` WHERE `id` = {$row['uid']}");
                                        $username = $user[0]['name'];
                                        print <<<table
        <tr>
		  <td>{$row['uid']}</td>
		  <td>{$username}</td>
          <td>{$row['day']}</td>
          <td><a href="dynamic_detail.php?aid={$row['id']}" target="_blank">{$content}</a></td>
		  <td>{$btn}</td>
        </tr>
table;
                                    } ?>
                                    </tbody>
                            </table>
                        </div>
                        </form>
                    </div>
                    <div class="btn-group pull-right">
                                <h4>第<?php
                                    if ($_GET['page']) {
                                        echo $_GET['page'];
                                    } else {
                                        echo 1;
                                    }
                                    ?>页</h4>
                            </div>
                            <div class="btn-group pull-right">
                                <a href="publish_list.php" class="btn" style="margin-left:10px;">首页</a></button>
                                <a href="publish_list.php?page=<?php echo $page_1.'&order='.$_GET['order'].'&typeid='.$_GET['typeid'] ?>" class="btn"
                                style="margin-left:5px;">上一页</a>
                                <a href="publish_list.php?page=<?php echo $page_2.'&order='.$_GET['order'].'&typeid='.$_GET['typeid'] ?>" class="btn"
                                   style="margin-left:5px;">下一页</a>
                            </div>
                    <div class="tab-pane fade" id="profile">
                    </div>
                </div>

            </div>

        </div>
    </div>

</div>

<footer>
    <hr>

    <p class="pull-right"><!--power by right--></p>


    <p><!--power by --></p>
</footer>
<script src="lib/bootstrap/js/bootstrap.js"></script>
</body>
</html>